package bdgrade;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JTextPane;

import domain.Stu;
import service.StuService;
import service.Impl.StuServiceImpl;
import tools.WriteTool;

import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.awt.GridLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.BorderLayout;

public class DeriveTips {
	StuService stuService=new StuServiceImpl();//注入service
	WriteTool wt=new WriteTool();
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DeriveTips window = new DeriveTips();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DeriveTips() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setVisible(true);
		frame.getContentPane().setBackground(Color.BLACK);
		frame.setBounds(100, 100, 628, 478);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.BLACK);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		JTextPane txtpntxttxt = new JTextPane();
		txtpntxttxt.setText("我将把学生信息导出为txt文本文件,请选择一个txt文件进行接收。");
		txtpntxttxt.setFont(new Font("宋体", Font.PLAIN, 18));
		txtpntxttxt.setForeground(Color.WHITE);
		txtpntxttxt.setBackground(Color.BLACK);
		txtpntxttxt.setBounds(36, 13, 537, 50);
		panel.add(txtpntxttxt);
		
		
		
		
		JButton btnNewButton_1 = new JButton("返回");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.dispose();
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 15));
		btnNewButton_1.setForeground(Color.WHITE);
		btnNewButton_1.setBackground(Color.BLACK);
		btnNewButton_1.setBounds(309, 345, 113, 50);
		panel.add(btnNewButton_1);
		JButton btnNewButton = new JButton("接收文件");
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 15));
		btnNewButton.setForeground(Color.WHITE);
		btnNewButton.setBackground(Color.BLACK);
		btnNewButton.setBounds(164, 345, 123, 50);
		panel.add(btnNewButton);
		btnNewButton.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				String path = null;
				JFileChooser fc = new JFileChooser();
				fc.setDialogTitle("请选择要导入的学生信息(txt文件)");
				fc.setApproveButtonText("确定");
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(panel)) {
				path=fc.getSelectedFile().getPath();
				}
				System.out.println(path);//C:\Users\Administrator\Desktop\a.txt
				wt.setPath(path);
				List<Stu> list = stuService.findAll();
				wt.setList(list);
				boolean flag=wt.writeAsFile();
				if(flag) {
					btnNewButton_1.setBackground(Color.GREEN);				
				}
				else btnNewButton_1.setBackground(Color.RED);
			}
		});
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(DeriveTips.class.getResource("/img/7.jpg")));
		label.setBounds(14, 76, 432, 266);
		panel.add(label);
	}
}
